<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Danhmuc_model extends CI_Model {


	var $madm = '';
	var $tendanhmuc = '';
	var $hinh = '';
	var $tendanhmuc_en = '';
	var $madm_cha = '';	
	
    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
		$this->load->database();
    }
	 
	function insert($data)
    {
		if($data['madm_cha'] == 0) $data['madm_cha'] = null;
        $this->db->insert('danhmuc', $data);
    }
	
	function getresult()
	{
		 $this->db->select("madm,tendanhmuc");
         $query=$this->db->get("danhmuc");
         return $query->result_array();
	}
	
	function getresult_index()
	{
		$data= $this->input->get();
		$this->db->select("madm,tendanhmuc,hinh,madm_cha,tendanhmuc_en");
		$query;
		if(!empty($data['dm'])&&!empty($data['key']))
		{
				
		$query =$this->db->query("SELECT d2.madm,d2.tendanhmuc,d2.hinh,d1.tendanhmuc as tendanhmuc_cha,d2.tendanhmuc_en FROM `danhmuc` d1 right join `danhmuc` d2 on d1.madm=d2.madm_cha WHERE d2.madm_cha ='".$data['dm']."' AND d2.tendanhmuc  like '%".$data['key']."%'");      
				
		}
		else if(!empty($data['dm']))
		{
			$query =$this->db->query("SELECT d2.madm,d2.tendanhmuc,d2.hinh,d1.tendanhmuc as tendanhmuc_cha,d2.tendanhmuc_en FROM `danhmuc` d1 right join `danhmuc` d2 on d1.madm=d2.madm_cha WHERE d2.madm_cha ='".$data['dm']."' ");           
		}else if(!empty($data['key']))
		{
		$query =$this->db->query("SELECT d2.madm,d2.tendanhmuc,d2.hinh,d1.tendanhmuc as tendanhmuc_cha,d2.tendanhmuc_en FROM `danhmuc` d1 right join `danhmuc` d2 on d1.madm=d2.madm_cha WHERE  d2.tendanhmuc  like '%".$data['key']."%'");      
		}else
		{
		$query =$this->db->query("SELECT d2.madm,d2.tendanhmuc,d2.hinh,d1.tendanhmuc as tendanhmuc_cha,d2.tendanhmuc_en FROM `danhmuc` d1 right join `danhmuc` d2 on d1.madm=d2.madm_cha");      	
		}
		return $query->result_array();
	}
	
	function getresult_vi_en()
	{
		 $this->db->select("madm,tendanhmuc,tendanhmuc_en");
         $query=$this->db->get("danhmuc");
       	 return $query->result_array();
	}
	
	function xoahinhdanhmuc($madm)
	{
		$row = $this->db->query("SELECT `hinh` FROM `danhmuc` WHERE `madm`='".$madm."'");
		$db1 = $row->row_array();
		
		if(!empty($db1))
		{
			$myFile = $db1['hinh'];
			$myFile = str_replace("/", "\\", $myFile);
			$unlinkdir = getcwd().'\\'.$myFile; 
			unlink($unlinkdir);
		}
	}
	
	function update($data, $madm)
    {
		$this->db->where("madm", $madm);
      	return $this->db->update('danhmuc', $data);
    }
	
	function getdanhmuc($madm)
	{
		$this->db->select("madm,tendanhmuc,hinh,madm_cha,tendanhmuc_en");
		$this->db->where("madm", $madm);
        $query = $this->db->get("danhmuc");
       	return $query->row_array();
	}
	
	function delete($madm)
    {
		$this->xoahinhdanhmuc($madm);
        $this->db->delete('danhmuc', "`madm` = '".$madm."'");
    }
	
	function LayDM()
	{
		$query=$this->db->query("SELECT DanhMuc.* , (SELECT COUNT(MaSP) FROM SanPham WHERE SanPham.Ma_DM=DanhMuc.MaDM ) AS Dem FROM   DanhMuc") ;
       	return $query->result_array();
	 }
}
?>